#!/usr/bin/env bash
###############################################################################
# copy / duplicate
#
# A plugin for `nb` providing copy / duplicate functionality.
#
# Install with:
#   nb plugin install https://github.com/xwmx/nb/blob/master/plugins/copy.nb-plugin
#
# https://github.com/xwmx/nb
###############################################################################

# Add the new subcommand names with `_subcommands add <name>`.
_subcommands add "copy"
_subcommands add "duplicate"

# Define help and usage text with `_subcommands describe <subcommand> <usage>`.
_subcommands describe "copy" <<HEREDOC
Usage:
  nb copy (<id> | <filename> | <path> | <title>)

Description:
  Create a copy of the specified item in the current notebook.

Alias: \`duplicate\`
HEREDOC

# Define the subcommand as a function, named with a leading underscore.
_copy() {
  # The selector argument can be an <id>, <filename>, <path>, or <title>, with
  # or without a preceeding notebook name. Examples:
  #   1
  #   example.md
  #   title
  #   /path/to/example.md
  #   notebook:1
  #   notebook:example.md
  #   notebook:title
  #   notebook:/path/to/example.md
  local _selector="${1:-}"

  # Check for the presence of a selector, exiting and printing help if not
  # found:
  if [[ -z "${_selector:-}" ]]
  then
    _help "copy"
    exit 1
  fi

  # Get the filename using the selector:
  local _source_filename
  _source_filename="$(_show "${_selector}" --filename)"

  # Get the current notebook path:
  local _notebook_path
  _notebook_path="$(_notebooks current --path)"

  # Validate that the selector represents a valid file:
  if [[ -z "${_source_filename:-}"                      ]] ||
     [[ ! -e "${_notebook_path}/${_source_filename}"    ]]
  then
    printf "Not found: %s\\n" "${_selector}"
    exit 1
  elif [[ ! -f "${_notebook_path}/${_source_filename}"  ]]
  then
    printf "Not a file: %s\\n" "${_selector}"
    exit 1
  fi

  # Get a unique filename based on the source filename:
  local _target_filename
  _target_filename="$(_notebooks current --filename "${_source_filename}")"

  # Check if the file is a text file:
  if _show "${_source_filename}" --type text
  then # Text can use `_add`.
    # Print the source contents and pipe to `_add`:
    _show "${_source_filename}" --no-color --print | _add "${_target_filename}"
  else # Binary files must be copied manually.
    # Copy the binary file to the new filename:
    cp                                        \
      "${_notebook_path}/${_source_filename}" \
      "${_notebook_path}/${_target_filename}"

    # Add the new filename to the index:
    _index add "${_target_filename}"

    # Record the change in git:
    _git checkpoint "[nb] Add: ${_target_filename}"

    # Print feedback:
    local _info
    _info="$(_show "${_target_filename}" --info-line)"

    printf "Added: %s\\n" "${_info}"
  fi
}

# Use `_subcommands alias` to define a full alias with linked help / usage.
# Aliases also have to be added with `_subcommands add <name>`.
_subcommands alias "copy" "duplicate"
